package com.example.apple.ddhr_new.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.donkingliang.groupedadapter.adapter.GroupedRecyclerViewAdapter;
import com.donkingliang.groupedadapter.holder.BaseViewHolder;

import java.util.ArrayList;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

public abstract class BaseAdapter<T> extends GroupedRecyclerViewAdapter {

    private ArrayList<T> data =new ArrayList<>();

    public ArrayList<T> getData() {
        return data;
    }

    public void setData(ArrayList<T> data) {
        this.data = data;
    }

    public BaseAdapter(Context context) {
        super(context);
    }

    @Override
    public int getGroupCount() {
        return 1;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return data.size();
    }

    @Override
    public boolean hasHeader(int groupPosition) {
        return false;
    }

    @Override
    public boolean hasFooter(int groupPosition) {
        return false;
    }

    @Override
    public int getHeaderLayout(int viewType) {
        return 0;
    }

    @Override
    public int getFooterLayout(int viewType) {
        return 0;
    }


    @Override
    public void onBindHeaderViewHolder(com.donkingliang.groupedadapter.holder.BaseViewHolder holder, int groupPosition) {

    }

    @Override
    public void onBindFooterViewHolder(com.donkingliang.groupedadapter.holder.BaseViewHolder holder, int groupPosition) {

    }
    @Override
    public int getChildViewType(int groupPosition, int childPosition) {
        return TYPE_CHILD;
    }


    @Override
    public void onBindChildViewHolder(com.donkingliang.groupedadapter.holder.BaseViewHolder holder, int groupPosition, int childPosition) {

        BaseAdapter.BaseViewHolder baseViewHolder = (BaseAdapter.BaseViewHolder)holder;

        baseViewHolder.setData(data.get(childPosition));
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(mContext).inflate(getChildLayout(viewType), parent, false);
        RecyclerView.ViewHolder holder = getViewHolder(viewType, view);

        if(holder==null){
            return super.onCreateViewHolder(parent,viewType);
        }
        return holder;
    }

    public abstract  BaseViewHolder<T> getViewHolder(int viewType,View view);
    public  abstract class   BaseViewHolder<T> extends  com.donkingliang.groupedadapter.holder.BaseViewHolder{

        public BaseViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
        public abstract void setData(T data);
    }
}
